<?php

namespace ApiBundle\Controller\Admin;

use ApiBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class ProjectController extends BaseController
{
    /*
     * 编辑项目
     */
    public function editProjectAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $id = $request->get("id");
        $name = $request->get("name");
        $type = $request->get("type");
        $attachment_id = $request->get("attachment_id");

        $process = $request->get("process","[]");

        $process_list = json_decode($process);

        if(empty($name) || empty($type) || empty($attachment_id) || empty($process)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");

        $data = array(
            "name" => $name,
            "type" => $type,
            "attachment_id" => $attachment_id,
            "admin_id" => $this->getAdminId()
        );

        // 添加项目
        if($id == '0' || empty($id)){
            $data['created_at'] = date("Y-m-d H:i:s");

            $data['district_id'] = $this->getDistrictId();

            // TODO: 项目表插入数据
            $conn->insert("project",$data);
            $project_id = $conn->lastInsertId();

        } else {
            $data['updated_at'] = date("Y-m-d H:i:s");
            $where = array(
                "id" => $id
            );
            // TODO: 项目表更新数据
            $conn->update("project",$data,$where);

            //TODO: 删除项目进度的旧数据
            $conn->delete("project_progress",array("project_id" => $id));

            $project_id = $id;
        }

        // TODO: 项目进度插入数据
        foreach($process_list as $item){
            $data_progress = array(
                "project_id" => $project_id,
                "name" => $item->name,
                "start_at" => $item->start_at,
                "end_at" => $item->end_at
            );
            $conn->insert("project_progress",$data_progress);
        }

        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 项目列表
     */
    public function projectListAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

        $conn = $this->get("database_connection");

        $admin_role_id = $this->get("session")->get("admin_role_id");

        if($admin_role_id == 4){
            $init_sql = "SELECT * FROM project WHERE admin_id = '". $this->getAdminId() ."'";

        } else {
            $init_sql = "SELECT * FROM project WHERE district_id = '". $this->getDistrictId() ."'";
        }

        //总数
        $count = count($conn->fetchAll($init_sql));

        // 今日新增项目
        $now_date = date("Y-m-d");
        $new_sql =  $init_sql . " AND DATE_FORMAT(created_at,'%Y-%m-%d') = '". $now_date ."' ";
        $new_count = count($conn->fetchAll($new_sql));


        // TODO 搜索
        $search_sql = $init_sql;
        $search = $request->get("search_keyword");
        if(!empty($search)){

            $search_sql .= " AND name LIKE '%". $search ."%' ";
        }

        // 带搜索条件下的总数
        $search_all_count = count($conn->fetchAll($search_sql));
        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $search_sql = $search_sql . " LIMIT $m,$rows ";

        $data = $conn->fetchAll($search_sql);

        $eve_page_count = count($data);

        $curr_date = date("Y-m-d");

        for($i=0;$i<$eve_page_count;$i++){
            $info = $conn->fetchAll("SELECT * FROM project_progress WHERE project_id = '". $data[$i]['id'] ."'");
            $data[$i]['progress_info'] = $info;

            // 获取当前进度
            $curr_progress = array();

            foreach ($info as $key => $value){

                $start_time = date('Y-m-d', strtotime($value['start_at']));
                $end_time = date('Y-m-d', strtotime($value['end_at']));

                if($curr_date >= $start_time && $curr_date <= $end_time){

                    array_push($curr_progress,$value['name']);
                }
            }

            $data[$i]['curr_progress_info'] = $curr_progress;
        }

        return $this->jsonResponse(0,"操作成功",array("data"=>$data,"allcount"=>$count,"newcount"=>$new_count,"search_all_count"=>$search_all_count,"all_page"=>$all_page,"curr_date"=>date('Y-m-d')));
    }

    /*
     * 项目详情
     */
    public function detailProjectAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        $id = $request->get("id");
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");
        $data = $conn->fetchAssoc("SELECT * FROM project WHERE id = '". $id ."'");

        $data['progress'] = $conn->fetchAll("SELECT * FROM project_progress WHERE project_id = '". $id ."' ");

        $data['attachment'] = $conn->fetchAssoc("SELECT * FROM attachment WHERE id = '". $data["attachment_id"] ."' ");

        return $this->jsonResponse(0,"操作成功",$data);

    }

    /*
     * 项目删除
     */
    public function deleteProjectAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->getAdminId();
        }

        $id = $request->get("id");
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");

        $where = array(
            "id" => $id
        );

        $conn->delete("project_progress",array("project_id" => $id));
        $conn->delete("project",$where);

        return $this->jsonResponse(0,"操作成功");

    }

}
